import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, Index, ManyToOne, JoinColumn } from 'typeorm';
import { User } from './User';

// 支付状态枚举
export enum PaymentStatus {
  PENDING = 0,     // 待支付
  PAID = 1,        // 已支付
  CANCELLED = 2,   // 已取消
  FAILED = 3,      // 支付失败
  REFUNDING = 4,   // 退款中
  REFUNDED = 5     // 已退款
}

// 支付方式枚举
export enum PaymentMethod {
  WECHAT = 0,      // 微信支付
  ALIPAY = 1,      // 支付宝
  BANK_TRANSFER = 2, // 银行转账
  CASH = 3,        // 现金
  OTHER = 99       // 其他
}

@Entity('payment_records')
export class PaymentRecord {
  @PrimaryGeneratedColumn({ type: 'bigint', unsigned: true, comment: '支付记录ID' })
  id!: number;

  @Column({ type: 'varchar', length: 50, name: 'order_no', comment: '订单编号' })
  @Index('idx_order_no', { unique: true })
  orderNo!: string;

  @Column({ type: 'varchar', length: 100, name: 'payment_item', comment: '支付项目' })
  paymentItem!: string;

  @Column({ type: 'varchar', length: 255, nullable: true, comment: '项目说明' })
  description?: string;

  @Column({ type: 'decimal', precision: 10, scale: 2, comment: '支付金额' })
  amount!: number;

  @Column({ type: 'varchar', length: 20, name: 'user_id', comment: '用户ID' })
  @Index('idx_user_id')
  userId!: string;

  @ManyToOne(() => User)
  @JoinColumn({ name: 'user_id' })
  user?: User;

  @Column({ 
    type: 'tinyint', 
    default: PaymentStatus.PENDING, 
    comment: '支付状态：0 待支付 1 已支付 2 已取消 3 支付失败 4 退款中 5 已退款' 
  })
  status!: PaymentStatus;

  @Column({ 
    type: 'tinyint', 
    nullable: true, 
    name: 'payment_method',
    comment: '支付方式：0 微信支付 1 支付宝 2 银行转账 3 现金 99 其他' 
  })
  paymentMethod?: PaymentMethod;

  @Column({ type: 'varchar', length: 100, nullable: true, name: 'transaction_id', comment: '交易号' })
  transactionId?: string;

  @Column({ type: 'timestamp', nullable: true, name: 'payment_time', comment: '支付时间' })
  paymentTime?: Date;

  @Column({ type: 'varchar', length: 255, nullable: true, comment: '备注' })
  remarks?: string;

  @Column({ type: 'varchar', length: 100, nullable: true, name: 'payment_proof', comment: '支付凭证' })
  paymentProof?: string;

  @Column({ type: 'varchar', length: 50, nullable: true, name: 'payer_name', comment: '支付人姓名' })
  payerName?: string;

  @Column({ type: 'varchar', length: 50, nullable: true, name: 'payer_contact', comment: '支付人联系方式' })
  payerContact?: string;

  @CreateDateColumn({ 
    type: 'timestamp',
    name: 'create_time',
    comment: '创建时间'
  })
  createTime!: Date;

  @UpdateDateColumn({ 
    type: 'timestamp',
    name: 'update_time',
    comment: '修改时间'
  })
  updateTime!: Date;

  @Column({ type: 'varchar', length: 50, nullable: true, name: 'created_by', comment: '创建人' })
  createdBy?: string;

  @Column({ type: 'varchar', length: 50, nullable: true, name: 'updated_by', comment: '修改人' })
  updatedBy?: string;

  constructor(data?: Partial<PaymentRecord>) {
    if (data) {
      Object.assign(this, data);
    }
  }

  get statusText(): string {
    const statusMap: { [key in PaymentStatus]: string } = {
      [PaymentStatus.PENDING]: '待支付',
      [PaymentStatus.PAID]: '已支付',
      [PaymentStatus.CANCELLED]: '已取消',
      [PaymentStatus.FAILED]: '支付失败',
      [PaymentStatus.REFUNDING]: '退款中',
      [PaymentStatus.REFUNDED]: '已退款'
    };
    return statusMap[this.status ?? PaymentStatus.PENDING];
  }

  get paymentMethodText(): string {
    const methodMap: { [key in PaymentMethod]: string } = {
      [PaymentMethod.WECHAT]: '微信支付',
      [PaymentMethod.ALIPAY]: '支付宝',
      [PaymentMethod.BANK_TRANSFER]: '银行转账',
      [PaymentMethod.CASH]: '现金',
      [PaymentMethod.OTHER]: '其他'
    };
    return this.paymentMethod !== undefined ? methodMap[this.paymentMethod] : '未知';
  }
} 